package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ruihang.generalibrary.ui.widget.CustomFontEditText;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.AddrInfo;
import com.sctvcloud.dongqu.beans.AddrInfoItem;
import com.sctvcloud.dongqu.beans.FUsers;
import com.sctvcloud.dongqu.beans.SingleResult;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.http.ParamsEditor;
import com.sctvcloud.dongqu.ui.utils.TitleUtils;
import com.sctvcloud.dongqu.utils.UserManager;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class AddressAddActivity extends BaseActivity {
    @BindView(R.id.edt_address_add_user)
    protected CustomFontEditText edtUsername;
    @BindView(R.id.edt_address_add_phone)
    protected CustomFontEditText edtPhone;
    @BindView(R.id.edt_address_add_info)
    protected CustomFontEditText edtAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle(R.string.add_new_address_title);
    }

    @Override
    protected void initInject() {
        initButterknifeBiner(R.layout.activity_address_add);
    }

    @OnClick(R.id.bt_addr_add)
    protected void OnAddClick(View v) {
        switch (v.getId()) {
            case R.id.bt_addr_add:
                String userName = edtUsername.getText().toString();
                if (TextUtils.isEmpty(userName)) {
                    toast("收货人姓名不能为空");
                    edtUsername.requestFocus();
                    return;
                }
                String phone = edtPhone.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("联系电话不能为空");
                    edtPhone.requestFocus();
                    return;
                }else if(phone.length()!=11){
                    toast("请输入11位有效的手机号");
                    edtPhone.requestFocus();
                    return;
                }
                String address = edtAddress.getText().toString();
                if (TextUtils.isEmpty(address)) {
                    toast("详细地址不能为空");
                    edtAddress.requestFocus();
                    return;
                }
                addAddressInfo(userName, phone, address);
                break;
        }
    }

    private void addAddressInfo(final String userName, final String phone, final String address) {
        FUsers fu = UserManager.getInstance().getUser();
        if (fu == null) {
            toLogin();
            return;
        }
//        ParamsEditor pe = new ParamsEditor();
//        pe.put("token", fu.getToken());
//        pe.put("userId", fu.getUserId());
//        pe.put("address", address);
//        pe.put("phone", phone);
//        pe.put("userName", userName);
        HashMap<String, Object> parm = new HashMap<>();
        parm.put("token", fu.getToken());
        parm.put("userId", fu.getPhoneNumber());
        parm.put("address", address);
        parm.put("phone", phone);
        parm.put("receiver", userName);
        NetUtils.getNetAdapter().addAddrInfo(getOwnerName(), parm, new AbsNetCallBack<AddrInfoItem>(AddrInfoItem.class) {
            @Override
            public void onSuc(AddrInfoItem singleResult) {
                if (singleResult.isSuccess()) {
//                    toast("添加成功");
                    Intent intent = new Intent();
                    intent.putExtra(EX_TYPE, true);
                    AddrInfo info = new AddrInfo();
                    info.setStatus(1);
                    info.setReceiver(userName);
                    info.setPhone(phone);
                    info.setAddress(address);
                    info.setAddrId(singleResult.getAddrId());
                    intent.putExtra(EX_DATA, info);
                    setResult(989, intent);
                    finish();
                } else {
                    toast("添加失败");
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
            }
        });
    }
}
